<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('ustcStudentInfoStudyExp.fields.accountId')" prop="accountId">
        <el-input
          v-model="formData.accountId"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.accountId')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoStudyExp.fields.sort')" prop="sort">
        <el-input
          v-model="formData.sort"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.sort')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoStudyExp.fields.schoolName')" prop="schoolName">
        <el-input
          v-model="formData.schoolName"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.schoolName')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoStudyExp.fields.startDate')" prop="startDate">
        <el-date-picker
          v-model="formData.startDate"
          type="date"
          value-format="x"
          :placeholder="
            t('common.selectTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.startDate')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoStudyExp.fields.endDate')" prop="endDate">
        <el-date-picker
          v-model="formData.endDate"
          type="date"
          value-format="x"
          :placeholder="
            t('common.selectTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.endDate')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoStudyExp.fields.majorName')" prop="majorName">
        <el-input
          v-model="formData.majorName"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.majorName')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoStudyExp.fields.majorDesc')" prop="majorDesc">
        <el-input
          v-model="formData.majorDesc"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.majorDesc')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoStudyExp.fields.degree')" prop="degree">
        <el-input
          v-model="formData.degree"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.degree')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoStudyExp.fields.degreeType')" prop="degreeType">
        <el-select
          v-model="formData.degreeType"
          :placeholder="
            t('common.selectTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.degreeType')
          "
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item
        :label="t('ustcStudentInfoStudyExp.fields.schoolLocation')"
        prop="schoolLocation"
      >
        <el-input
          v-model="formData.schoolLocation"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.schoolLocation')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoStudyExp.fields.studyDuration')" prop="studyDuration">
        <el-input
          v-model="formData.studyDuration"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.studyDuration')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoStudyExp.fields.graduated')" prop="graduated">
        <el-input
          v-model="formData.graduated"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.graduated')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoStudyExp.fields.certifier')" prop="certifier">
        <el-input
          v-model="formData.certifier"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.certifier')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoStudyExp.fields.certifierUnit')" prop="certifierUnit">
        <el-input
          v-model="formData.certifierUnit"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.certifierUnit')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcStudentInfoStudyExp.fields.remark')" prop="remark">
        <el-input
          v-model="formData.remark"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.remark')
          "
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import {
  UstcStudentInfoStudyExpApi,
  UstcStudentInfoStudyExpVO
} from '@/api/core/ustcstudentinfostudyexp'
import { FormRules } from 'element-plus'

/** 学生教育经历 表单 */
defineOptions({ name: 'StudentInfoStudyExpForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  accountId: undefined,
  sort: undefined,
  schoolName: undefined,
  startDate: undefined,
  endDate: undefined,
  majorName: undefined,
  majorDesc: undefined,
  degree: undefined,
  degreeType: undefined,
  schoolLocation: undefined,
  studyDuration: undefined,
  graduated: undefined,
  certifier: undefined,
  certifierUnit: undefined,
  remark: undefined
})
const formRules = reactive<FormRules>({
  id: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.id'),
      trigger: 'blur'
    }
  ],
  accountId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.accountId'),
      trigger: 'blur'
    }
  ],
  sort: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.sort'),
      trigger: 'blur'
    }
  ],
  schoolName: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.schoolName'),
      trigger: 'blur'
    }
  ],
  startDate: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.startDate'),
      trigger: 'blur'
    }
  ],
  endDate: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.endDate'),
      trigger: 'blur'
    }
  ],
  majorName: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.majorName'),
      trigger: 'blur'
    }
  ],
  majorDesc: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.majorDesc'),
      trigger: 'blur'
    }
  ],
  degree: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.degree'),
      trigger: 'blur'
    }
  ],
  degreeType: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.degreeType'),
      trigger: 'change'
    }
  ],
  schoolLocation: [
    {
      required: true,
      message:
        t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.schoolLocation'),
      trigger: 'blur'
    }
  ],
  studyDuration: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.studyDuration'),
      trigger: 'blur'
    }
  ],
  graduated: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.graduated'),
      trigger: 'blur'
    }
  ],
  certifier: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.certifier'),
      trigger: 'blur'
    }
  ],
  certifierUnit: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.certifierUnit'),
      trigger: 'blur'
    }
  ],
  creator: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.creator'),
      trigger: 'blur'
    }
  ],
  createTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.createTime'),
      trigger: 'blur'
    }
  ],
  updater: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.updater'),
      trigger: 'blur'
    }
  ],
  updateTime: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.updateTime'),
      trigger: 'blur'
    }
  ],
  deleted: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.deleted'),
      trigger: 'blur'
    }
  ],
  tenantId: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcStudentInfoStudyExp.fields.tenantId'),
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await UstcStudentInfoStudyExpApi.getUstcStudentInfoStudyExp(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as UstcStudentInfoStudyExpVO
    if (formType.value === 'create') {
      await UstcStudentInfoStudyExpApi.createUstcStudentInfoStudyExp(data)
      message.success(t('common.createSuccess'))
    } else {
      await UstcStudentInfoStudyExpApi.updateUstcStudentInfoStudyExp(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    accountId: undefined,
    sort: undefined,
    schoolName: undefined,
    startDate: undefined,
    endDate: undefined,
    majorName: undefined,
    majorDesc: undefined,
    degree: undefined,
    degreeType: undefined,
    schoolLocation: undefined,
    studyDuration: undefined,
    graduated: undefined,
    certifier: undefined,
    certifierUnit: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}
</script>
